package coml.jiyun.hp.gjtk_e;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import coml.jiyun.hp.gjtk_e.adapter.TabAdapter;
import coml.jiyun.hp.gjtk_e.bean.TabBean;
import coml.jiyun.hp.gjtk_e.fragment.HomeFragment;
import coml.jiyun.hp.gjtk_e.net.ApiService;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TabLayout mTab;
    private ViewPager mVp;
    private TabLayout mMainTab;
    private ViewPager mMainVp;
    /**
     * 查看收藏
     */
    private Button mMainBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiService.BSER_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        ApiService service = retrofit.create(ApiService.class);
        Observable<TabBean> observable = service.getData();
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TabBean>() {

                    private TabAdapter adapter;

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(TabBean tabBean) {
                        ArrayList<TabBean.DataBean> dataBeans = (ArrayList<TabBean.DataBean>) tabBean.getData();
                        ArrayList<Fragment> fragments = new ArrayList<>();
                        for (int i = 0; i < dataBeans.size(); i++) {
                            HomeFragment homeFragment = new HomeFragment();
                            Bundle bundle = new Bundle();
                            bundle.putInt("id", dataBeans.get(i).getId());
                            homeFragment.setArguments(bundle);
                            fragments.add(homeFragment);
                        }

                        adapter = new TabAdapter(getSupportFragmentManager(), fragments, dataBeans);
                        mVp.setAdapter(adapter);
                        mTab.setupWithViewPager(mVp);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initView() {
        mTab = (TabLayout) findViewById(R.id.main_tab);
        mVp = (ViewPager) findViewById(R.id.main_vp);
        mMainTab = (TabLayout) findViewById(R.id.main_tab);
        mMainVp = (ViewPager) findViewById(R.id.main_vp);
        mMainBtn = (Button) findViewById(R.id.main_btn);
        mMainBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.main_btn:
                break;
        }
    }
}